package com.powernode.controller;

import com.powernode.dto.PageResult;
import com.powernode.param.ProductQueryParam;
import com.powernode.dto.Result;
import com.powernode.entity.Product;
import com.powernode.enums.PicDirectory;
import com.powernode.enums.ProductStatus;
import com.powernode.service.ProductService;
import com.powernode.util.MinioUtil;
import com.powernode.validation.SaveGroup;
import com.powernode.validation.UpdateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/pms/admin/products")
public class AdminProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private MinioUtil minioUtil;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) {
        return minioUtil.upload(file, PicDirectory.PRODUCT.getDirectory());
    }
    @DeleteMapping("/remove")
    public Result removeFile(@RequestParam String filename){
        return minioUtil.removeFile(filename);
    }

    @DeleteMapping("/removeBatch")
    public Result removeBatch(@RequestParam List<String> files) throws Exception{
        return minioUtil.removeBatch(files);
    }

    /**
    * 根据商品id单查
    */
    @GetMapping("/{productId}")
    public Result getById(@PathVariable("productId") Long id){
        return Result.success(productService.getProductById(id));
    }
    /**
    * 商品分页
    */
    @GetMapping
    public Result<PageResult<Product>> getPage(@Validated ProductQueryParam queryParam){
        return Result.success(productService.pageQuery(queryParam));
    }
    /**
    * 商品新增
     * @Validated(SaveGroup.class) 只校验新增校验组的数据和没有设置校验组的数据
    */
    @PostMapping
    public Result save(@RequestBody @Validated(SaveGroup.class) Product product){
        //mybatis-plus在执行新增时如果表主键是自增的，会自动将自增的主键id更新到实体对象中
        //在执行新增之前，id为null
        //System.out.println("product = " + product);
        productService.saveProduct(product);
        //执行新增之后，传入的实体对象会自动更新id属性
        //System.out.println("product = " + product);
        return Result.success("商品新增成功", product.getId());
    }


    /**
     * 商品状态更新
     **/
    @PutMapping("status/{productId}/{status}")
    public Result updateProductStatus(@PathVariable Long productId,@PathVariable Integer status){
        productService.updateProductStatus(productId,status);
        String msg = ProductStatus.ON.getCode() == status ? "商品状态已更改为：上架" : "商品状态已更改为：下架";
        return Result.success(msg,null);
    }

    /**
    * 商品编辑
    */
    @PutMapping
    public Result edit(@RequestBody @Validated(UpdateGroup.class) Product product){
        productService.updateProduct(product);
        return Result.success("商品修改成功",null);
    }

    /**
    * 商品单删
    */
    @DeleteMapping("/{productId}")
    public Result removeProduct(@PathVariable Long productId){
        productService.removeProductById(productId);
        return Result.success("商品删除成功",null);
    }
}
